import {Component, OnInit} from "@angular/core";
import {PersonalMessage} from "../entity/PersonalMessage";
import {UserService} from "../service/user.service";
import {NgForm} from "@angular/forms";
import { FileUploader } from 'ng2-file-upload';
import {CommonIp} from "../entity/Ip";
import {AddressItem} from "../entity/AddressItem";
@Component({
  selector:'personalsetmessage',
  templateUrl:'./personal-set.html',
  styleUrls: ['./personal_set_people.css']
})
export class personalSetComponent extends CommonIp implements OnInit{

  public personalSetMessages :PersonalMessage=new PersonalMessage();
   public countrys:Array<string>;
  public provinces:Array<string>;
  public citys:Array<string>;
  public areas:Array<string>;

  private headersParam:Array<any> = new Array<any>();
  public  uploader:FileUploader;
  constructor(private userservice:UserService){
    super();
    this.uploader = new FileUploader({

      url: this.userIp+"/fileUpload",

      method: "POST",
      itemAlias: "uploadedfile",
      removeAfterUpload:true
    });
  }





  selectedFileOnChanged() {
    // 这里是文件选择完成后的操作处理
    this.uploader.queue[0].upload(); // 开始上传

    this.uploader.queue[0].onSuccess = (response, status, headers) => {
      // 上传文件成功
      if (status == 200) {
        // 上传文件后获取服务器返回的数据

        let tempRes = JSON.parse(response);
        document.getElementById("imgsrc").setAttribute("src",this.userimgIp+tempRes.fileUrl);
        this.personalSetMessages.photo=tempRes.fileUrl;

      }else {
        // 上传文件后获取服务器返回的数据错误
      }
    };


  }




  public SelectProvince(country:string){


    this.userservice.getProvince(country).then(provinces=>this.provinces=provinces);
  }
  public SelectCitys(province:string){

    this.userservice.getCity(province).then(citys=>this.citys=citys);
  }
  public SelectArea(city:string){
    this.userservice.getArea(city).then(areas=>this.areas=areas

    )
  }
  private getPersonalSetMessage(userid:string){
    //noinspection TypeScriptUnresolvedFunction
    this.userservice.getPersonalSetMessage(userid)
      .then(personalSetMessages=>this.personalSetMessages=personalSetMessages)

  }

  ngOnInit(): void {

    if(sessionStorage.getItem("userimg")!=null){

      document.getElementById("imgsrc").setAttribute("src",sessionStorage.getItem("userimg"));

    }
    if(sessionStorage.getItem("id")!=null){
      this.getPersonalSetMessage(sessionStorage.getItem("id"));
    }


    this.SelectProvince("中国");

  }
  public getPersaonlSetUpdateMessage(personalSetMessage:PersonalMessage){
    this.userservice.getPersonalSetUpdateMessage(personalSetMessage);
  }


  onSubmit(personalsetFrom:NgForm,personalSetMessages:PersonalMessage){

    var name= (<HTMLInputElement>document.getElementById("name")).value;
    var birthday=(<HTMLInputElement>document.getElementById("birthday")).value;

    var province=(<HTMLInputElement>document.getElementById("province")).value;
    var city=(<HTMLInputElement>document.getElementById("city")).value;
    var area=(<HTMLInputElement>document.getElementById("area")).value;
    var profile=(<HTMLInputElement>document.getElementById("profile")).value;
    if(name==null||name==""){
      $.jBox.tip("请填写名字!");
    }
    else if(birthday==null||birthday==""){
      $.jBox.tip("请填写生日!");
    }

    else if(province==null||province==""){
      $.jBox.tip("请选择省!");
    }
    else if(area==null||area==""){
      $.jBox.tip("请选择地区!");
    }
    else if(profile==null||profile==""){
      $.jBox.tip("请填写简介!");
    }else {

      personalSetMessages.name=name;
      personalSetMessages.birthday=birthday;

      personalSetMessages.city=city;
      personalSetMessages.province=province;
      personalSetMessages.profile=profile;
      personalSetMessages.area=area;
      personalSetMessages.id=sessionStorage.getItem("id");
      sessionStorage.setItem("name",name);
      if(profile.length>200){
        $.jBox.tip("输入内容限制为200字以内,您输入的内容太长，请重新输入");
      }else {
        this.getPersaonlSetUpdateMessage(personalSetMessages);
      }


  }


  }
  // public getCountryList(){
  //   this.userservice.getCountry().then(countrys=>this.countrys=countrys);
  // }
}
